package com.lmy.timer;

import java.util.Date;
import java.util.TimerTask;

/**
 * 定义一个定时任务类
 * TimerTask实现了Runnable接口，可以在run()中编写定时任务逻辑
 */
public class LmyTimerTask extends TimerTask {

    private String name;

    public LmyTimerTask(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println("当前执行任务的线程名:" + Thread.currentThread().getName());
            System.out.println("name=" + name + ",startTime=" + new Date());
            Thread.sleep(3000);
            System.out.println("name=" + name + ",endTime=" + new Date());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
